<?php
class GroupAction extends BaseAction
{

	 protected function _initialize()
	 {
	 	parent::_initialize();
	 	$this->assign('current','group');  //头部导航切换
	 }

	//群首页
	public function index()
	{
		//群组微博
		$strType = h($_GET['type']);
        $data['type'] = $strType;
		$data['list'] = D('WeiboOperate')->getHomeList($this->mid, $this->gid, $strType, '', 10);
        $this->assign($data);
        
		//新加入的成员
		$newJoinList = D('Member')->where('gid='.$this->gid." AND level>0")->order('ctime DESC')->limit(4)->findAll();
		$this->assign('newJoinList',$newJoinList);
		$this->display();
	}

    //查找微博话题
    public function search()
    {
    	$data['search_key']    = $this->_getSearchKey();
        $data['type'] 		   = t($_REQUEST['type']);
    	$data['list'] 		   = D('WeiboOperate')->doSearch( $data['search_key'], $this->gid, $data['type'] );
		//$data['hotTopic'] 	   = D('WeiboTopic','weibo')->getHot();
        $data['search_key_id'] = D('WeiboTopic')->getTopicId($data['search_key'], $this->gid);
        //$data['followTopic']   = D('Follow','weibo')->getTopicList($this->mid);
    	$this->assign($data);
    	$this->display('index');
    }

    //查看微博详细 
    public function detail()
    {
	    $data['mini']      = D('GroupWeibo', 'group')->getOneLocation($_GET['id'], $this->gid);
	    if(!$data['mini']) $this->error('提交错误参数');
		$data['comment']   =  D('WeiboComment','weibo')->getComment($_GET['id'], $this->gid);
		$data['privacy'] = D('UserPrivacy','home')->getPrivacy($this->mid,$data['mini']['uid']);

    	$this->assign( $data );
    	$this->display();
    }

	// 加入该群
	public function  joinGroup()
	{
		if (isset($_POST['addsubmit'])) {
			$level = 0;
			$incMemberCount = false;
			if ($this->is_invited) {
				M('group_invite_verify')->where("gid={$this->gid} AND uid={$this->mid} AND is_used=0")->save(array('is_used'=>1));
				if (0 === intval($_POST['accept'])) {
					// 拒绝邀请
					exit;
				} else {
					// 接受邀请加入
		        	$level = 3;
					$incMemberCount = ture;
				}
        	} else if ($this->groupinfo['need_invite'] == 0) {
        		// 直接加入
				$level = 3;
				$incMemberCount = ture;
			} else if ($this->groupinfo['need_invite'] == 1) {
				// 需要审批，发送私信到管理员
				$level = 0;
				$incMemberCount = false;
        		// 添加通知
        		$toUserIds = D('Member')->field('uid')->where('gid='.$this->gid.' AND (level=1 or level=2)')->findAll();
        		foreach ($toUserIds as $k=>$v) {
					$toUserIds[$k] = $v['uid'];
				}
	
				$message_data['title']   = "申请加入群组 {$this->groupinfo['name']}";
				$message_data['content'] = "你好，请求你批准加入“{$this->groupinfo['name']}” 群组，点此"
										 . U('group/Manage/membermanage', array('gid'=>$this->gid,'type'=>'apply')) . '进行操作。';
				$message_data['to'] 	 = $toUserIds;
	            $res = model('Message')->postMessage($message_data,  $this->mid);

        	} 

			$result = D('Group')->joinGroup($this->mid, $this->gid, $level, $incMemberCount, $_POST['reason']);   //加入
			exit;
		}

		parent::base();

		$this->assign('joinCount', D('Member')->where("uid={$this->mid} AND level>1")->count());
		$member_info = D('Member')->field('level')->where("gid={$this->gid} AND uid={$this->mid}")->find();
		$this->assign('isjoin', $member_info['level']);  // 是否加入过或加入情况
		$this->display();
	}

	//退出该群对话框
	function quitGroupDialog() {
		$this->assign('gid',$this->gid);
		$this->display();
	}

	//退出该群
	function quitGroup() {
		if(iscreater($this->mid,$this->gid) || !$this->ismember) { echo '0';exit;} //$this->error('你没有权限'); //群组不可以退出
		$res = D('Member')->where("uid={$this->mid} AND gid={$this->gid}")->delete();  //用户退出
		if($res){
			D('Group')->setDec('membercount', 'id=' . $this->gid);     //用户数量减少1
			// 积分操作
			X('Credit')->setUserCredit($this->mid, 'quit_group');
			echo '1';
			exit;
		}
	}


	//删除该群
	function delGroup() {
		if (md5($_POST['verify']) != $_SESSION['verify']) {
			exit('验证码错误');
		}
		if(!iscreater($this->mid,$this->gid))  exit('你没有权限');
		D('Group')->remove($this->gid);
		exit('1');
	}


	//删除群组对话框
	function delGroupDialog() {

		$this->assign('gid',$this->gid);
		$this->display();
	}

	function addShare_check(){

		$result = 1;

		$aimId = intval($_REQUEST['aimId']);
		$this->assign('aimId',$aimId);		
			
		$test = $this->api->share_isForbid($this->mid,8,$aimId);

		if($test==-1){
			$result = -2;
		}	

        echo $result;
	}
	function addShare(){
		$aimId = intval($_REQUEST['aimId']);
		$this->assign('aimId',$aimId);
		$group = D('group')->where("id='$aimId'")->field('name')->find();

		$this->assign('name',$group['name']);
		$this->assign($group);
        $this->assign('mid',$this->mid);
		$this->display();
	}

	function doaddShare(){
		$type['typeId'] = 8;
		$type['typeName'] = '群组';
		$type['alias'] = 'group';

		$info = h($_REQUEST['info']);
		$aimId = intval($_REQUEST['aimId']);

		$field = 'uid,name,logo,cid0,membercount';
		$data = D('group')->where("id='$aimId'")->field($field)->find();
		$data['logo'] = get_photo_url($data['logo']);
		$data['catagory'] = D('Category')->where("id=".$data['cid0'])->getField('title');

		//$data['name'] = h($_REQUEST['name']);
		$fids = $_REQUEST['fids'];


		$result = $this->api->share_addShare($type,$aimId,$data,$info,0,$fids);
		echo $result;
	}


}